<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;

class Order extends Base
{
    public function index(){
        if (request()->isPost()){
            if(input('param.order_num')){
                $orders=Db::table('lg_order')
                    ->alias('a')
                    ->join('lg_user b ','b.user_id= a.order_user_id')
                    ->where('a.order_status','>=',1)
                    ->where('a.order_num',input('param.order_num'))
                    ->order('a.order_ctime','desc')
                    ->limit(200)
                    ->select();
            }elseif (input('param.phone')){
                $orders=Db::table('lg_order')
                    ->alias('a')
                    ->join('lg_user b ','b.user_id= a.order_user_id')
                    ->where('a.order_status','>=',1)
                    ->where('a.order_phone',input('param.phone'))
                    ->order('a.order_ctime','desc')
                    ->limit(200)
                    ->select();
            }elseif (input('param.start_date') && input('param.end_date')){
                $start=strtotime(input('param.start_date') . "00:00:00");
                $end = strtotime(input('param.end_date'). "23:59:59");
                $orders=Db::table('lg_order')
                    ->alias('a')
                    ->join('lg_user b ','b.user_id= a.order_user_id')
                    ->where('a.order_status','>=',1)
                    ->where('a.order_ctime','>',$start)
                    ->where('a.order_ctime','<',$end)
                    ->order('a.order_ctime','desc')
                    ->limit(200)
                    ->select();
            }else{
                $orders=Db::table('lg_order')
                    ->alias('a')
                    ->join('lg_user b ','b.user_id= a.order_user_id')
                    ->where('a.order_status','>=',1)
                    ->order('a.order_ctime','desc')
                    ->limit(200)
                    ->select();
                foreach ($orders as $k=>$v){
                    $peiId=\db('tuple')->where('tuple_order_id',$v['order_id'])->value('tuple_user_id');
                    if($peiId){
                        $orders[$k]['tuple_msg']=\db('user')->find($peiId);
                    }else{
                        $orders[$k]['tuple_msg']=null;
                    }
                }
            }
        }else{
            $orders=Db::table('lg_order')
                ->alias('a')
                ->join('lg_user b ','b.user_id= a.order_user_id')
                ->where('a.order_status','>=',1)
                ->order('a.order_ctime','desc')
                ->limit(200)
                ->select();

        }
        if($orders){
            foreach ($orders as $k=>$v){
                $peiId=\db('tuple')->where('tuple_order_id',$v['order_id'])->value('tuple_user_id');
                if($peiId){
                    $orders[$k]['tuple_msg']=\db('user')->find($peiId);
                }else{
                    $orders[$k]['tuple_msg']=null;
                }
            }
        }
        $this->assign('orders',$orders);
        return $this->fetch();
    }
    public function outOrder(){
        $orders=Db::table('lg_order')
            ->alias('a')
            ->join('lg_user b ','b.user_id= a.order_user_id')
            ->where('a.order_status','>',0)
            ->where('a.order_status','<',5)
            ->where('a.order_status','<>',2)
            ->where('a.order_status','<>',3)
            ->where('a.order_outtime','<',time())
            ->order('a.order_ctime','desc')
            ->limit(200)
            ->select();
        if($orders){
        foreach ($orders as $k=>$v){
            $peiId=\db('tuple')->where('tuple_order_id',$v['order_id'])->value('tuple_user_id');
            if($peiId){
                $orders[$k]['tuple_msg']=\db('user')->find($peiId);
            }else{
                $orders[$k]['tuple_msg']=null;
            }
            }
        }
        $this->assign('orders',$orders);
        return $this->fetch();
    }
    public function tuikuan(){
        if(input('param.order_num')){
            $order_num=input('param.order_num');
            $order=\db('order')->where('order_num',$order_num)->find();
            $result = $this->wxpay->refund->byOutTradeNumber($order['order_num'], $order['order_num'], $order['order_price']*100, $order['order_price']*100, [
                // 可在此处传入其他参数，详细参数见微信支付文档
                'refund_desc' => '订单超时退款',
            ]);
            if($result['result_code']=="SUCCESS"){
                $upOrder=\db('order')->where('order_num',$order_num)->update(['order_status'=>4,'order_otime'=>time()]);
                if($upOrder){
                    return json(['status'=>1,'msg'=>'退款成功']);
                }else{
                    return json(['status'=>0,'msg'=>'退款失败']);
                }
            }else{
                return json(['status'=>0,'msg'=>$result['err_code_des']]);
            }
        }


    }

}
